#!/bin/bash

gcc -v 2> /dev/null
if [[ $? != 0 ]]; then
	echo "unable to find gcc"
	exit
fi
if [[ ! -d /opt/google/chrome ]]; then
	echo "unable to find chrome"
	exit
fi

gcc -O2 -Wall -fPIC -ldl -pthread -shared -o spork-tweak.so spork-tweak.c

IFS=''
: > google-chrome
while read line; do
	if [[ "$line" == *LD_PRELOAD* ]]; then
		continue
	fi
	if [[ "$line" == exec* ]]; then
		echo "export LD_PRELOAD=spork-tweak.so" >> google-chrome
	fi
	echo "$line"                                    >> google-chrome
done < /opt/google/chrome/google-chrome
chmod +x google-chrome

su -c "cp spork-tweak.so google-chrome /opt/google/chrome"

rm spork-tweak.so
rm google-chrome
